package com.lzj.weather01;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Weather01Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        try {
            //替换双引号，并按照逗号对数据进行切分
            String[] datas = value.toString().replaceAll("\"", "").split(",");
            if (datas != null && datas.length == 11) {
                //获取数据
                String adcode = datas[3];
                String temperature = datas[5];
                String reporttime = datas[9];
                Date date = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(reporttime);
                //将数据写出 Key 110101:20201018 value 28
                context.write(new Text(adcode + ":" + new SimpleDateFormat("yyyyMMdd").format(date)), new IntWritable(Integer.parseInt(temperature)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
